
var selectedLocation = -1;
var showDebug = false;

var locationKey = new Array("water",
"land","hill","mountain","spawn");

function makeArray(str) {
  return str.split(/ /);
}

// Returns the item in the array, else returns false
function in_array(needle, haystack) {
  debugLog(true, true, "IN::in_array::looking for '"+ needle + "' in '" + haystack + "'");
  var found = false;
  for (index in haystack) {
    if (haystack[index] == needle) {
      found = haystack[index];
    }
  }
  debugLog(true, true, "OUT::in_array::return '" + found + "'");
  return found;
}

function addClass(classList, newClass) {
  return classList + " " + newClass;
}

function removeClass(classList, oldClass) {
  debugLog(true, true, "IN::removeClass::removing '" + oldClass + "' from '" + classList + "'");
  var classes = classList.split(/ /);
  var returnClass = "";
  for (index in classes) {
    if (classes[index] != oldClass) {
      returnClass += classes[index] + " ";
    }
  }
  debugLog(true, true, "OUT::removeClass::return '" + returnClass + "'");
  return returnClass;
}

function makeSelected(classList) {
  var classes = classList.split(/ /);
  var returnClass = "";
  for (var index in classes) {
    if (in_array(classes[index], locationKey) != false) {
      returnClass += classes[index] + "Selected ";
    } else {
      returnClass += classes[index] + " ";
    }
  }
  return returnClass
}

function makeUnselected(classList) {
  var classes = classList.split(/ /);
  var returnClass = "";
  for (var index in classes) {
    var selectedIndex = classes[index].indexOf("Selected");
    if (selectedIndex != -1) {
      returnClass += classes[index].substr(0,(classes[index].length - 8));
    } else {
      returnClass += classes[index] + " ";
    }
  }
  return returnClass
}

function selectLocation(index) {
  var location = document.getElementById("location[" + index + "]");
  if (location) {
    if (selectedLocation != -1) {
      var oldLocation = document.getElementById("location[" + selectedLocation + "]");
      if (document.getElementById("locationValueU[" + selectedLocation + "]").value != 0) {
        moveUnits(selectedLocation, index);
      }
      oldLocation.className = makeUnselected(oldLocation.className);
    }
  
    selectedLocation = index;
    location.className = makeSelected(location.className);
  }
}

function checkDistance(startLocation, endLocation) {
  debugLog(true, true, "IN::checkDistance::start '" + startLocation + "' end '" + endLocation + "'");
  startLocation = parseInt(startLocation);
  endLocation = parseInt(endLocation);
  
  var validLocation = false;
  var validList = new Array(startLocation + 5,startLocation + 6,startLocation + 11,startLocation - 5,startLocation - 6,startLocation - 11);
    
  debugLog(true, true, "IN::checkDistance::validList '" + validList + "'");
  validLocation = in_array(endLocation, validList);
  
  debugLog(true, true, "OUT::checkDistance::return '" + validLocation + "'");
  return validLocation;
}

function moveUnits(startLocation, endLocation) {
  // Check the end location is valid
  var location = document.getElementById("location[" + endLocation + "]");
  if (location) {
    if (startLocation != endLocation) {
      if (in_array("water", makeArray(location.className)) != "water") {
        if (checkDistance(startLocation, endLocation) != false) {
          var oActionPoints = document.getElementById("locationValueA[" + startLocation + "]");
          var iActionPoints = oActionPoints.value;
          if (parseInt(iActionPoints) > 0) {
            var oUnits = document.getElementById("locationValueU[" + startLocation + "]");
            var iUnitValue = oUnits.value;
            if (iUnitValue != 0) {
              var oTeam = document.getElementById("locationValueT[" + startLocation + "]");
              var sTeamValue = oTeam.value
              //if (sTeamValue == playersTeam) {
              
                // Move off
                oldLocationText = document.getElementById("locationText[" + startLocation + "]");
                oldLocationText.innerHTML = "";
                oldLocationText.className = removeClass(oldLocationText.className, sTeamValue);
                oldLocationText.className = addClass(oldLocationText.className, "none");
                oUnits.value = "0";
                oTeam.value = "";  
                oActionPoints.value = "";
                
                // Move on
                document.getElementById("locationValueU[" + endLocation + "]").value = iUnitValue;
                document.getElementById("locationValueT[" + endLocation + "]").value = sTeamValue;
                document.getElementById("locationValueA[" + endLocation + "]").value = (parseInt(iActionPoints) - 1);
                locationText = document.getElementById("locationText[" + endLocation + "]");
                locationText.innerHTML = iUnitValue;
                locationText.className = removeClass(locationText.className, "none");
                locationText.className = addClass(locationText.className, sTeamValue);
                
                debugLog(false, true, iUnitValue + " moves " + sTeamValue + " from " + startLocation + " to " + endLocation);
              //} else {
              //  moveCompleted(false, "Select units are not yours to move");
              //}
            } else {
              debugLog(false, false, "No units available to move");
            }
          } else {
            debugLog(false, false, "No more action points");
          }
        } else {
          debugLog(false, false, "Unable to move that distance");
        }
      } else {
        debugLog(false, false, "Unable to move onto water");
      }
    }
  } else {
    debugLog(false, false, "Not valid location");
  }
}

function debugLog(debugMsg, bool, msg) {
  var displayClass = "";
  if (debugMsg) {
    displayClass = "debug"
  }
  if (debugMsg == true && showDebug == true || debugMsg == false) {
    var currentLog = document.getElementById("gameLog").innerHTML;
     document.getElementById("gameLog").innerHTML = "<p class=\"" + displayClass + "\">" + msg + "</p>" + currentLog;
  }
}

function setLocationUnitCount(index, units, team) {
  var location = document.getElementById("location[" + index + "]");
  if (location) {
    var locationValueU = document.getElementById("locationValueU[" + index + "]").value = units;
    var locationValueT = document.getElementById("locationValueT[" + index + "]").value = team;
    var locationText = document.getElementById("locationText[" + index + "]").innerHTML = units;
  }
}
